<?php

	/**
	* Manage get json all event
	* return in json format
	* 
	* @author trungdt
	* @since Nov 4, 2011
	* 
	* Change logs
	* Nov 1, 2011 - trungdt - Change Edit action for new template
	*/

	if (!defined('EXPONENT')) exit('');
	
	$locsql = "({$db->prefix}calendar.location_data='".serialize($loc)."'";
	$config = $db->selectObject("calendarmodule_config","location_data='".serialize($loc)."'");
	if (!empty($config->aggregate)) {
		$locations = unserialize($config->aggregate);
		foreach ($locations as $source) {
			$tmploc = null;
			$tmploc->mod = 'calendarmodule';
			$tmploc->src = $source;
			$tmploc->int = '';
			$locsql .= " OR {$db->prefix}calendar.location_data='".serialize($tmploc)."'";
		}
	}
	$locsql .= ')';
	
	$permission_approve=exponent_permissions_check(array("administrate", "approve"), $loc);

	$sql_where=" AND (approve = 2)";
	if ($permission_approve)
		$sql_where="";

	// sort
	$sort="";
	if (isset($_REQUEST['sort']))
	{
		$sort=$_REQUEST['sort'];
		// for some field has been faked, we should turn them into the right field name
		if ($sort == "posted_text")
			$sort="posted";
		else if ($sort == "eventstart_text")
				$sort = "eventstart";
			else if ($sort == "eventend_text")
					$sort = "eventend";
				else if ($sort == "approve_date_text")
						$sort = "approve_date";
					else if ($sort == "category")
							$sort = "category_id";
						$sortOrder="ASC";
		if (isset($_REQUEST['order']) && ($_REQUEST['order'] != "asc"))
			$sortOrder = "DESC";
		$sort = " ORDER BY {$sort} {$sortOrder}";
	}
	else
		$sort="ORDER BY approve, posted DESC";

	// pagination
	$page=1;
	if (isset($_REQUEST['page']))
		$page=intval($_REQUEST['page']);
	$pageSize=10;
	if (isset($_REQUEST['pageSize']))
		$pageSize=intval($_REQUEST['pageSize']);
	$start=($page-1)*$pageSize;

	$data=null;

	$sql="SELECT {$db->prefix}calendar.*, {$db->prefix}category.name category 
	FROM 
	{$db->prefix}calendar INNER JOIN 
	{$db->prefix}category
	ON 
	({$db->prefix}calendar.location_data = {$db->prefix}category.location_data) AND ({$db->prefix}calendar.category_id = {$db->prefix}category.id)

	WHERE {$locsql} {$sql_where}

	{$sort}

	LIMIT {$start}, {$pageSize}

	";

	$data=$db->selectObjectsBySql($sql);

	//$data=$db->selectObjects("calendar", "location_data = '{$sloc}' ORDER BY approve");
	foreach ($data as $k=>$v){
		$v->eventstart_text=date('M j, Y, H:i',$v->eventstart);
		$v->eventend_text=date('M j, Y, H:i',$v->eventend);
		$v->posted_text=date('M j, Y, H:i:s',$v->posted);
		$v->edited_text=date('M j, Y, H:i:s',$v->edited);

		if ($v->approve >  0){
			$v->approve_date_text=date('M j, Y, H:i:s',$v->approve_date);
		}
		else
			$v->approve_date_text = "";
		//
		$v->body_splited=substr($v->body, 0, 100);
		//
		$temp=exponent_users_getUserById($v->poster);
		$v->poster="{$temp->firstname} {$temp->lastname}({$temp->username})";
	}
	// prepare for return json
	$ret=null;
	$ret->total=count($data);
	$ret->rows=$data;
	echo json_encode($ret);
?>
